/**
 * 弹出框
 * @author heke
 * @version 20170330
 */
import _ from 'lodash'
import _uuid from 'lodash-uuid'

const state = {
    alerts: [],
    alertDefault: {
        type: 'default',
        gid: '',
        style: 'success', // success | info | warning | danger
        text: '',
        timeout: 5000,
        callback: []
    },
    alertConfirm: {
        type: 'confirm',
        gid: '',
        style: 'success',
        text: '',
        callbacks: {
            ok: {
                text: '确定',
                callback: []
            },
            cancel: {
                text: '取消',
                callback: ['dismiss']
            }
        }
    },
    isBackdropIn: false
}

const getters = {
    alerts: state => state.alerts,
    isBackdropIn: state => state.isBackdropIn
}

const actions = {
    addAlertDefault ({ commit }, options) {
        commit('ADD_ALERT_DEFAULT', { options })
    },
    addAlertConfirm ({ commit }, options) {
        commit('ADD_ALERT_CONFIRM', { options })
        commit('SHOW_BACKDROP')
    },
    removeAlert ({ commit }, gid) {
        commit('REMOVE_ALERT', { gid })
    },
    showBackdrop ({ commit }) {
        commit('SHOW_BACKDROP')
    },
    hideBackdrop ({ commit }) {
        commit('HIDE_BACKDROP')
    },
    showHelp ({ commit }, gid) {
        commit('SHOW_HELP', { gid })
    }
}

const mutations = {
    'ADD_ALERT_DEFAULT' (state, { options }) {
        try {
            // 校验 style 是否 string
            if (!_.isObjectLike(options)) throw new Error('parameter style must be Object')
            // 新建实例
            let newAlert = _.merge(_.cloneDeep(state.alertDefault), options)
            // 生成新的 gid
            newAlert.gid = _uuid.uuid()
            // 限制总数为50个
            if (state.alerts.length === 10) state.alerts = []
            // 添加到队列
            state.alerts.push(newAlert)
            // 打印到控制台
            console.info(newAlert.text)
        } catch (ex) {
            console.error(ex.message)
        }
    },
    'ADD_ALERT_CONFIRM' (state, { options }) {
        try {
            // 校验 style 是否 string
            if (!_.isObjectLike(options)) throw new Error('parameter style must be Object')
            // 新建实例
            let newAlert = _.merge(_.cloneDeep(state.alertConfirm), options)
            // 生成新的 gid
            newAlert.gid = _uuid.uuid()
            // 限制总数为10个
            if (state.alerts.length === 10) state.alerts = []
            // 添加到队列
            state.alerts.push(newAlert)
            // 打印到控制台
            console.info(newAlert.text)
        } catch (ex) {
            console.error(ex.message)
        }
    },
    'REMOVE_ALERT' (state, { gid }) {
        try {
            let currentAlertIndex = _.findIndex(state.alerts, { gid: gid })
            if (currentAlertIndex === -1) throw new Error(`Alert (gid: ${gid}) has been removed.`)
            _.remove(state.alerts, item => {
                return item.gid === gid
            })
        } catch (ex) {
            console.error(ex.message)
        }
    },
    'SHOW_BACKDROP' (state) {
        state.isBackdropIn = true
    },
    'HIDE_BACKDROP' (state) {
        state.isBackdropIn = false
    },
    'SHOW_HELP' (state, { gid }) {
        console.log('show help ' + gid)
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
